package com.xiaoxx.service.impl;

import com.xiaoxx.mapper.CodeGenerationMapper;
import com.xiaoxx.model.entity.code.Fileds;
import com.xiaoxx.model.entity.code.Tables;
import com.xiaoxx.model.vo.code.FiledsVo;
import com.xiaoxx.model.vo.code.TablesVo;
import com.xiaoxx.service.CodeGenerationService;
import com.xiaoxx.utils.BeanDtoVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CodeGenerationServiceImpl implements CodeGenerationService {
    @Autowired
    private CodeGenerationMapper codeGenerationMapper;

    @Override
    public List<TablesVo> getTables(String name) {
        List<Tables> tables = codeGenerationMapper.getTables(name);
        return BeanDtoVoUtil.listVo(tables, TablesVo.class);
    }

    @Override
    public List<FiledsVo> getTableFileds(String name) {
        List<FiledsVo> vos = new ArrayList<>();
        List<Fileds> tableFileds = codeGenerationMapper.getTableFileds(name);
        for (Fileds tableFiled : tableFileds) {
            FiledsVo vo = new FiledsVo();
            vo.setFieldName(tableFiled.getField());
            vo.setIsNull(tableFiled.getNull());
            vo.setFieldType(tableFiled.getType());
            vo.setFieldKey(tableFiled.getKey());
            vo.setDefaultValue(tableFiled.getDefault());
            vo.setComment(tableFiled.getComment());
            vos.add(vo);
        }
        return vos;
    }
}
